package com.codeczx.animationdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TweenActivity extends AppCompatActivity {

    private static final String TAG = "TweenActivity";
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.btn_translate)
    Button btnTranslate;
    private int fromDelta = 0;
    private int toXDelta = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.imageView, R.id.btn_translate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imageView:
                Toast.makeText(this, "miyo!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_translate:
                final Animation translateAnimation = new TranslateAnimation(fromDelta, toXDelta, 0, 0);
                translateAnimation.setDuration(3000);
                translateAnimation.setFillAfter(true);
                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        imageView.clearAnimation();
                        int left = imageView.getLeft();
                        int right = imageView.getRight();
                        int top  = imageView.getTop();
                        int bottom = imageView.getBottom();
                        imageView.layout(left+toXDelta,top,right+toXDelta,bottom);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                imageView.startAnimation(translateAnimation);
                break;
        }
    }
}
